/*
 * Copyright (c) 2019 Hitevision
 */

package com.hht.wbjni;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;

import tw.com.hitevision.whiteboard.android.Props;
import tw.com.hitevision.whiteboard.android.util.SystemPropertiesUtils;

public class WbJniCall {
    private static boolean isLoaded = false;
    private static final String TAG = WbJniCall.class.getSimpleName();

    static {
        if (Props.isPaintHelperEnabled()) {
            try {
                /*isLoad为true时，表示应用直接用jni调用加速，为false时，表示通过service启动另外一个apk调用加速*/
                if (Props.getMainboardVersion() == Props.Mainboard.V_811) {
                    Log.d(TAG, "static initializer: load: hb811");
                    System.loadLibrary("hb811");
                    isLoaded = true;
                } else if(Props.getFlavorChipset().equals(Props.Flavor.Chipset.Amlogic)){
                    Log.d(TAG, "static initializer: load: speed" );
                    System.loadLibrary("speed");
                    isLoaded = true;
                }else {
                    Log.d(TAG, "static initializer: load: whiteboard_celerate" );
                    System.loadLibrary("whiteboard_celerate");
                    if(SystemPropertiesUtils.getStringValue(SystemPropertiesUtils.KEY_8386_19A_OR_19B, "0").contains(Props.OEM_Model.MODEL_19B)
                            || Props.getFlavorChipset().equals(Props.Flavor.Chipset.Optoma))
                        isLoaded = true;
                }
                Log.d(TAG, "static initializer: isLoaded= " + isLoaded);
            } catch (UnsatisfiedLinkError e) {
                e.printStackTrace();
                Log.d(TAG, "static initializer: load fail.");
                isLoaded = false;
            }
        }
    }

    public static boolean isLoaded() {
        return isLoaded;
    }

    public static native void fbStart();

    public static native void fbStartDraw();

    public static native void fbStop();

    public static native void fbRefresh(int delayMs);

    public static native void fbClear();

    public static native void fbSetPaintColor(int color, int deviceType);

    public static native void fbSetPaintStrokeWidth(float stroke, int deviceType);

    public static native void fbDrawPoint(float x, float y, int deviceType);

    public static native void fbMoveTo(float x, float y, int fingerId, int deviceType);

    public static native void fbLineTo(float x, float y, int fingerId, int deviceType);

    public static native void fbQuadto(float x1, float y1, float x2, float y2, int fingerId, int deviceType);
    public static native void fbReadData(String filepath, int x, int y, int w, int h);

    public static native void fbDrawRect(float x, float y, float w, float h, int color);
    public static native void fbDrawCircle(float x, float y, int r, int color);

    public static native void fbShowGop(boolean isShow);

    public static native boolean fbDrawBitmap(Bitmap bitmap, int left, int top, int right, int bottom);

    public static native boolean fbDrawBitmap2(Bitmap bitmap, int left, int top, int right, int bottom, int frameWidth, int offX, int offY);

    public static native boolean fbDrawBitmap3(Bitmap bitmap);

    public static native void fbSetCanvas(Canvas canvas);

    public static native long fbGetFbAddr();

    public static native boolean nativeDrawBitmapRect(Bitmap bmp, int left, int top, int right, int botom, int left_des, int top_des, int right_des, int bottom_des);

    public static native void fbDrawCircle(float x, float y, float r, int id);

    public static native boolean fbDrawScreenRect(Bitmap bmp, int x, int y, int w, int h);

    public static native boolean fbDrawScreenRect2(Bitmap bmp, float x, float y, float w, float h, float limit, boolean isFirst, boolean drawCircle);

//    JNIEXPORT void JNICALL Java_com_hht_wbjni_WbJniCall_fbSetCanvas(JNIEnv *env, jobject activity, jobject canvas)
//    JNIEXPORT jlong JNICALL Java_com_hht_wbjni_WbJniCall_fbGetFbAddr(JNIEnv *env, jobject obj)

//    JNIEXPORT jboolean JNICALL Java_com_hht_wbjni_WbJniCall_fbDrawBitmap(
//            JNIEnv *env, jobject obj, jobject bitmap, int left, int top, int right, int bottom);
//
//    JNIEXPORT jboolean JNICALL Java_com_hht_wbjni_WbJniCall_fbDrawBitmap2( JNIEnv *env, jobject obj,
//                                                                           jobject bitmap, int left, int top, int right, int bottom, int frameWidth, int offX, int offY);
//
//    JNIEXPORT jboolean JNICALL Java_com_hht_wbjni_WbJniCall_fbDrawBitmap3( JNIEnv *env, jobject obj, jobject bitmap );
}
